package org.jeecg.config.sharding;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;

import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;

import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;

import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;

import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter;
import org.jeecg.config.util.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

@Configuration
public class DataSourceConfig {

    /**
     * 分表数据源名称
     */
    private static final String SHARDING_DATA_SOURCE_NAME = "sharding";
    /**
     * shardingjdbc有四种数据源，需要根据业务注入不同的数据源
     *
     * <p>1. 未使用分片, 脱敏的名称(默认): shardingDataSource;
     * <p>2. 主从数据源: masterSlaveDataSource;
     * <p>3. 脱敏数据源：encryptDataSource;
     * <p>4. 影子数据源：shadowDataSource
     *
     */
    @Lazy
    @Resource(name ="shardingDataSource")
    AbstractDataSourceAdapter shardingDataSource;
    @Autowired
    DynamicDataSourceProvider   dynamicDataSourceProvider ;

    /**
     * 动态数据源配置项
     */
    @Autowired
    private DynamicDataSourceProperties properties;

    Map<String, DataSource> dataSourceMap = new HashMap<>();




/*    @Bean
 public    ShardingSphereDataSource getShardingSphereDataSource() throws SQLException {
        Collection<RuleConfiguration> shardingRuleConfigurations = new ArrayList();





        ShardingSphereDataSourceFactory.createDataSource(dataSourceMap,shardingRuleConfigurations,new Properties());

    }*/

/*
    private static Map<String, DataSource> createDataSourceMap() {
        final Map<String, DataSource> result = new HashMap<>();
        result.put("demo_ds_master_0", DataSourceUtil.createDataSource("demo_ds_master_0"));

        return result;
    }
*/




    /**
     * 直接生成数据源
     * @return
     */
  /*  @Bean
    public DynamicDataSourceProvider dynamicDataSourceProvider() {

       //手动构建 dataSourceMap
        for (Map.Entry<String, DataSourceProperty> stringDataSourcePropertyEntry : properties.getDatasource().entrySet()) {
            DruidDataSourceCreator dataSource = new DruidDataSourceCreator(properties.getDruid());
            dataSourceMap.put(stringDataSourcePropertyEntry.getKey(), dataSource.createDataSource(stringDataSourcePropertyEntry.getValue()));
        }
        //将这个转为数据源
        DynamicDataSourceProvider dynamicDataSourceProvider = () -> {
            dataSourceMap.put("sharding-datasource", shardingDataSource);
            return dataSourceMap;
        };
        return dynamicDataSourceProvider;

    }*/




 /*   @Bean
    public DynamicRoutingDataSource dynamicDataSource(DataSource shardingDataSource) {
        // 2. 将 ShardingDataSource 作为 dynamic-datasource 的子数据源
        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
        dynamicDataSource.addDataSource("sharding-datasource",shardingDataSource);
        dynamicDataSource.setPrimary("sharding-datasource"); // 设置默认路由
        return dynamicDataSource;
    }*/

    /**
     * 将动态数据源设置为首选的
     * 当spring存在多个数据源时, 自动注入的是首选的对象
     * 设置为主要的数据源之后，就可以支持shardingjdbc原生的配置方式了
     *
     * @return
     */
    @Primary
    @Bean
    public DataSource dataSource() {
        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
        properties.getDatasource().forEach((key, prop) -> {
            DruidDataSourceCreator creator = new DruidDataSourceCreator(properties.getDruid());
            dynamicDataSource.addDataSource(key, creator.createDataSource(prop));
        });
        // 2. 添加 ShardingSphere 分片数据源
        dynamicDataSource.addDataSource(SHARDING_DATA_SOURCE_NAME, shardingDataSource);
        // 3. 设置默认数据源和策略
        dynamicDataSource.setPrimary(properties.getPrimary());
        return dynamicDataSource;
    }

}
